﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
using Microsoft.Office.Interop.Excel;

namespace Export
{
	public static class ExcelExport
	{
		public static void SaveToExcelFile(ExcelRAExportView ra, string fileName)
		{
			var excelApp = new Excel.Application();

			var wb = excelApp.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
			var ws = (Excel.Worksheet)wb.ActiveSheet;

			excelApp.ScreenUpdating = false;

			// Заголовок
			SetTextBlock(ref ws, 1, 1, 1, 8, string.Format("Платежное поручение №{0}", ra.Number),
			             XlHAlign.xlHAlignCenter, true, false, true);

			// Дата выставления
			SetTextBlock(ref ws, 2, 1, 1, 4, string.Format("Дата выставления:"),
						 XlHAlign.xlHAlignLeft, false, true, false);
			SetTextBlock(ref ws, 3, 1, 1, 4, string.Format("{0}", ra.Date),
			 XlHAlign.xlHAlignRight, false, false, false);


			// Стутус
			SetTextBlock(ref ws, 2, 5, 1, 4, string.Format("Статус:"),
						 XlHAlign.xlHAlignLeft, false, true, false);
			SetTextBlock(ref ws, 3, 5, 1, 4, string.Format("{0}", ra.Status),
			 XlHAlign.xlHAlignRight, false, false, false);

			// Сумма
			SetTextBlock(ref ws, 4, 1, 1, 4, string.Format("Сумма:"),
						 XlHAlign.xlHAlignLeft, false, true, false);
			SetTextBlock(ref ws, 5, 1, 1, 4, string.Format("{0}", ra.Amount),
			 XlHAlign.xlHAlignRight, false, false, false);

			// Валюта
			SetTextBlock(ref ws, 4, 5, 1, 4, string.Format("Валюта:"),
						 XlHAlign.xlHAlignLeft, false, true, false);
			SetTextBlock(ref ws, 5, 5, 1, 4, string.Format("{0}", ra.Currency),
			 XlHAlign.xlHAlignRight, false, false, false);

			// Назначение платежа
			SetTextBlock(ref ws, 6, 1, 1, 8, string.Format("Назначение платежа:"),
						 XlHAlign.xlHAlignLeft, false, true, false);
			SetTextBlock(ref ws, 7, 1, 2, 8, string.Format("{0}", ra.Description),
			 XlHAlign.xlHAlignLeft, false, false, false);

			// Имя отправителя
			SetTextBlock(ref ws, 9, 1, 1, 4, string.Format("Имя отправителя:"),
						 XlHAlign.xlHAlignLeft, false, true, false);
			SetTextBlock(ref ws, 10, 1, 1, 4, string.Format("{0}", ra.PayerName),
			 XlHAlign.xlHAlignRight, false, false, false);

			// РС отправителя
			SetTextBlock(ref ws, 9, 5, 1, 4, string.Format("Р/c отправителя:"),
						 XlHAlign.xlHAlignLeft, false, true, false);
			SetTextBlock(ref ws, 10, 5, 1, 4, string.Format("{0}", ra.PayerAccount),
			 XlHAlign.xlHAlignRight, false, false, false);

			// Имя получателя
			SetTextBlock(ref ws, 11, 1, 1, 4, string.Format("Имя получателя:"),
						 XlHAlign.xlHAlignLeft, false, true, false);
			SetTextBlock(ref ws, 12, 1, 1, 4, string.Format("{0}", ra.BillToName),
			 XlHAlign.xlHAlignRight, false, false, false);

			// РС получателя
			SetTextBlock(ref ws, 11, 5, 1, 4, string.Format("Р/c получателя:"),
						 XlHAlign.xlHAlignLeft, false, true, false);
			SetTextBlock(ref ws, 12, 5, 1, 4, string.Format("{0}", ra.BillToAccount),
			 XlHAlign.xlHAlignRight, false, false, false);

			wb.SaveAs(fileName,
			          Excel.XlFileFormat.xlWorkbookNormal,
			          Missing.Value,
			          Missing.Value,
			          Missing.Value,
			          Missing.Value,
			          Excel.XlSaveAsAccessMode.xlExclusive,
			          Missing.Value,
			          Missing.Value,
			          Missing.Value,
			          Missing.Value,
			          Missing.Value);

			
			excelApp.Quit();
		}

		private static void SetTextBlock(
			ref Excel.Worksheet ws,
			int indR,
			int indC,
			int offsetR,
			int offsetC,
			string text,
			XlHAlign halign,
			bool isBold,
			bool isDashBottom,
			bool isHeader
			)
		{
			ws.Cells[indR, indC] = text;
			var rg = ws.get_Range(ws.Cells[indR, indC], ws.Cells[indR + offsetR - 1, indC + offsetC - 1]);
			rg.Merge("true");
			rg.Font.Bold = isBold;
			rg.HorizontalAlignment = halign;

			if (isHeader)
				rg.Borders[XlBordersIndex.xlEdgeTop].LineStyle = XlLineStyle.xlContinuous;
			rg.Borders[XlBordersIndex.xlEdgeLeft].LineStyle = XlLineStyle.xlContinuous;
			rg.Borders[XlBordersIndex.xlEdgeRight].LineStyle = XlLineStyle.xlContinuous;
			if (isDashBottom)
				rg.Borders[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlDash;
			else rg.Borders[XlBordersIndex.xlEdgeBottom].LineStyle = XlLineStyle.xlContinuous;

		}
	}
}